package com.example.gokchinesefoodmapmcdev.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.gokchinesefoodmapmcdev.dao.OrderInfoDAO;
import com.example.gokchinesefoodmapmcdev.dao.OrderSkuDAO;
import com.example.gokchinesefoodmapmcdev.dao.ReviewInfoDAO;
import com.example.gokchinesefoodmapmcdev.dao.SysUserDAO;
import com.example.gokchinesefoodmapmcdev.entity.order.OrderInfo;
import com.example.gokchinesefoodmapmcdev.entity.order.OrderSku;
import com.example.gokchinesefoodmapmcdev.entity.review.ReviewInfo;
import com.example.gokchinesefoodmapmcdev.entity.sys.SysUser;
import com.example.gokchinesefoodmapmcdev.service.ReviewService;
import com.example.gokchinesefoodmapmcdev.util.MyTranslateTools;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ReviewServiceImpl implements ReviewService {

    @Autowired
    private OrderInfoDAO orderInfoDAO;

    @Autowired
    private OrderSkuDAO orderSkuDAO;

    @Autowired
    private ReviewInfoDAO reviewInfoDAO;

    @Autowired
    private SysUserDAO sysUserDAO;

    /**
     * 添加一条评论信息业务处理
     * @param reviewInfo  前端数据封装的评论对象，还需要进一步处理
     * @return
     */
    @Override
    public Integer addReviewInfo(ReviewInfo reviewInfo, Integer orderId) {
        /*
                接收到信息包含四个评分信息，和一个订单id
                1. 添加其它数据：商品id、评论顾客id、创建/修改 时间、订单号
         */
        OrderInfo orderInfo = orderInfoDAO.selectOne(new QueryWrapper<OrderInfo>().eq("id", orderId));
        if(orderInfo == null){
            return 0;
        }
        reviewInfo.setCustomerId(orderInfo.getCustomerId());
        reviewInfo.setOrderNo(orderInfo.getOrderNo());

        // 注意这里的orderNo是从查到的订单信息中取，而不是从传入的参数，传入的参数是订单id
        OrderSku orderSku = orderSkuDAO.selectOne(new QueryWrapper<OrderSku>().eq("order_no", orderInfo.getOrderNo()));
        reviewInfo.setGoodId(orderSku.getGoodId());

        // 填充时间
        Date gmtCreate = new Date();
        reviewInfo.setGmtCreate(gmtCreate);
        reviewInfo.setGmtModified(gmtCreate);

        // 插入数据
        int result = reviewInfoDAO.insert(reviewInfo);
        return result;
    }

    /**
     * 通过商品ID获取评价列表
     *      还需要将评价用户的部分信息取出一起封装
     * @param goodId        商品id
     * @param pageNumber    当前页码数
     * @param pageSize      一页显示数量
     * @return
     */
    @Override
    public List<Map<String, Object>> getReviewInfoByGoodId(Integer goodId, Integer pageNumber, Integer pageSize) {
        // 1. 开启分页
        PageHelper.startPage(pageNumber, pageSize);

        // 2. 查询评价列表
        List<ReviewInfo> reviewInfos = reviewInfoDAO.selectList(new QueryWrapper<ReviewInfo>().eq("good_id", goodId));

        List<Map<String, Object>> result = new ArrayList<>();
        // 3. 遍历评价信息，获取用户信息
        for (ReviewInfo reviewInfo : reviewInfos) {
            Map<String, Object> map = MyTranslateTools.transEntityToMap(reviewInfo);
            // 查询用户信息
            SysUser sysUser = sysUserDAO.selectOne(new QueryWrapper<SysUser>().eq("user_id", reviewInfo.getCustomerId()));
            map.put("nickName", sysUser.getNickName());
            map.put("headimgurl", null);    // 数据库中没有头像字段的设置，暂时设置为空

            result.add(map);
        }

        return result;
    }
}
